﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace teacher.Util
{
    public class JsonpResult<T> : ActionResult
    {
        public T Obj { get; set; }
        public string CallbackName { get; set; }

        public JsonpResult(T obj, HttpRequestBase Request)
        {
            this.Obj = obj;
            this.CallbackName = (string)Request["jsoncallback"];
        }

        public override void ExecuteResult(ControllerContext context)
        {
            //var js = new System.Web.Script.Serialization.JavaScriptSerializer();
            var jsonp = Newtonsoft.Json.JsonConvert.SerializeObject(this.Obj);
            if (!string.IsNullOrEmpty(CallbackName))
            {
                jsonp = this.CallbackName + "(" + jsonp + ")";
            }
            context.HttpContext.Response.ContentType = "application/json";
            context.HttpContext.Response.Write(jsonp);
        }
    }
}